Spring Cloud Consul হল একটি সার্ভিস ডিসকভারি এবং কনফিগারেশন ম্যানেজমেন্ট সিস্টেম যা Consul ব্যবহার করে মাইক্রোসার্ভিসগুলির জন্য একটি ডিস্ট্রিবিউটেড রেজিস্ট্রি সরবরাহ করে। এটি Service Discovery এবং Configuration Management সহ বিভিন্ন ফিচার সরবরাহ করে যা মাইক্রোসার্ভিস আর্কিটেকচারে অত্যন্ত গুরুত্বপূর্ণ।
এখানে আমরা একটি Spring Cloud Consul উদাহরণ তৈরি করবো, যেখানে একটি Service A এবং Service B তৈরি করা হবে। Service A কনফিগারেশন কনসুল থেকে লোড করবে এবং Service B সার্ভিস ডিসকভারি ব্যবহার করবে Consul এর মাধ্যমে।
Spring Cloud Consul উদাহরণ
এই উদাহরণে আমরা দুটি সার্ভিস তৈরি করবো:
- Service A – এটি Consul এর মাধ্যমে কনফিগারেশন লোড করবে।
- Service B – এটি Consul এর মাধ্যমে Service A কে ডিসকভার করবে এবং রিকোয়েস্ট পাঠাবে।
১. Maven Dependencies
Service A:
Service A সার্ভিসের জন্য Spring Cloud Consul এবং Spring Boot Starter Web ডিপেনডেন্সি যোগ করুন।
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Service B:
Service B সার্ভিসের জন্যও একই ডিপেনডেন্সি থাকতে হবে, তবে এখানে spring-cloud-starter-consul-discovery এবং RestTemplate এর জন্য ডিপেনডেন্সি ব্যবহার করা হবে।
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
২. Service A তৈরি করা
Service A একটি সাধারণ Spring Boot অ্যাপ্লিকেশন হবে যা Consul এর মাধ্যমে কনফিগারেশন রেজিস্টার করবে এবং /hello পাথে একটি বার্তা রিটার্ন করবে।
Service A - Controller:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ServiceAController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Service A!";
}
}
Service A - application.yml:
Service A এর কনফিগারেশন ফাইল (এই ফাইলটি Consul থেকে কনফিগারেশন রেজিস্টার করবে):
spring:
application:
name: service-a
server:
port: 8081
servlet:
context-path: /service-a
eureka:
client:
registerWithEureka: false
fetchRegistry: false
consul:
host: localhost
port: 8500
discovery:
enabled: true
service-name: service-a
এখানে Service A কনসুল রেজিস্ট্রি সার্ভিস হিসাবে কাজ করবে এবং Consul এর মাধ্যমে এটি নিজেকে সার্ভিস হিসেবে রেজিস্টার করবে।
৩. Service B তৈরি করা
Service B Consul এর মাধ্যমে Service A-কে ডিসকভার করবে এবং /fetch পাথে রিকোয়েস্ট পাঠাবে।
Service B - Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ServiceBController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/fetch")
public String fetchFromServiceA() {
String url = "http://service-a/service-a/hello"; // Service A is discovered by Consul
return restTemplate.getForObject(url, String.class);
}
}
Service B - application.yml:
Service B এর কনফিগারেশন ফাইল (এটি Consul এর মাধ্যমে Service A কে ডিসকভার করবে):
spring:
application:
name: service-b
server:
port: 8082
eureka:
client:
registerWithEureka: false
fetchRegistry: false
consul:
host: localhost
port: 8500
discovery:
enabled: true
service-name: service-b
Service B - RestTemplate Bean:
Service B এর মধ্যে RestTemplate bean কনফিগার করার জন্য:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
৪. Consul সার্ভার চালু করা
Consul চালু করতে আপনার মেশিনে Consul ইন্সটল করা থাকতে হবে অথবা আপনি Docker ব্যবহার করে এটি চালাতে পারেন।
Consul Docker কমান্ড (যদি প্রয়োজন হয়):
docker run -d -p 8500:8500 -p 8600:8600/udp --name consul consul
এটি Consul সার্ভার চালু করবে এবং আপনি http://localhost:8500 থেকে Consul UI দেখতে পারবেন।
৫. Service A এবং Service B চালানো
প্রথমে Service A চালু করুন:
mvn spring-boot:run -Dspring-boot.run.profiles=service-aতারপর Service B চালু করুন:
mvn spring-boot:run -Dspring-boot.run.profiles=service-b
৬. সার্ভিস ডিসকভারি এবং রিকোয়েস্ট
Service B-এর
/fetchরাউট থেকে রিকোয়েস্ট পাঠালে এটি Service A থেকে ডেটা গ্রহণ করবে।Service B -
/fetchপাথ টেস্ট করা:GET http://localhost:8082/fetch- এটি Service A থেকে
"Hello from Service A!"বার্তা রিটার্ন করবে।
৭. Consul UI
আপনি Consul UI থেকে সিস্টেমের সমস্ত সার্ভিসের অবস্থা এবং রেজিস্ট্রেশন দেখতে পারবেন:
- Consul UI দেখার জন্য http://localhost:8500 এ ব্রাউজ করুন।
- Service A এবং Service B উভয় সার্ভিসই Consul এ রেজিস্টার হওয়া উচিত এবং তাদের অবস্থা দেখা যাবে।
Conclusion
এভাবে Spring Cloud Consul ব্যবহার করে আপনি সহজেই Service Discovery এবং Configuration Management ইমপ্লিমেন্ট করতে পারেন। Service A এবং Service B উদাহরণে আমরা দেখেছি কীভাবে সার্ভিস ডিসকভারি এবং কনফিগারেশন ম্যানেজমেন্ট Consul এর মাধ্যমে পরিচালিত হতে পারে। Spring Cloud Consul-এ এই ফিচারগুলি মাইক্রোসার্ভিস আর্কিটেকচারের মধ্যে একটি অত্যন্ত গুরুত্বপূর্ণ ভূমিকা পালন করে।
Read more